import { Channel, ChannelsRespones } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import { http } from '@/utils/http'
import { differenceBy } from 'lodash'


// 本地存储 频道 的 key
const CHANNEL_KEY = 'geek-h5-channels'
// 获得频道数据
export const getChannels = (): RootThunkAction => {
  return async (dispatch, getState) => {
    /*
      1. 在该 action 中，判断用户是否登录 (有无token)
      2. 如果已登录，发送请求获取用户的频道列表数据
      3. 如果未登录，先判断本地缓存中有没有频道列表数据
      4. 如果有，拿到本地的频道列表数据
      5. 如果没有，就发送请求获取默认的频道列表数据，并存储到本地缓存中
    */
    const {
      login: { token },
    } = getState()
    console.log(token)

    // 1判断如果登录就发送请求
    if (!!token) {
      const res = await http.get<ChannelsRespones>('/user/channels')

      dispatch({
        type: 'Home/getChannels',
        payload: res.data.data.channels,
      })
    } else {
      //2没有登录就判断本地是否有数据
      const localstore = JSON.parse(
        localStorage.getItem(CHANNEL_KEY) ?? '[]'
      ) as Channel[]
      // 3没有数据,发送请求默认数据，存到本地
      if (localstore.length === 0) {
        const res = await http.get<ChannelsRespones>('/user/channels')
        const channels = res.data.data.channels

        console.log(channels)

        dispatch({
          type: 'Home/getChannels',
          payload: channels,
        })

        localStorage.setItem(CHANNEL_KEY, JSON.stringify(channels))
      } else {
        // 4 如果有就存到redux与本地中
        dispatch({ type: 'Home/getChannels', payload: localstore })
      }
    }
  }
}

// 切换高亮
export const changeChannels = (key: number): RootThunkAction => {
  return (dispatch) => {
    // console.log(key)
    dispatch({ type: 'Home/changeChannels', payload: key })
  }
}

// 获取所有的可选频道
export const getRestChannels = (): RootThunkAction => {
  return async (dispatch, getState) => {
    const res = await http.get<ChannelsRespones>('/channels')

    const {
      home: { channels },
    } = getState()

    // 使用difference方法排除已有channels
    const restChannels = differenceBy(res.data.data.channels, channels, 'id')
    // console.log(restChannels)
    dispatch({ type: 'Home/getRestChannels', payload: restChannels })
  }
}

// 删除我的频道
export const delMyChannels = (channel: Channel): RootThunkAction => {
  return async (dispatch, getState) => {
    // console.log(11)
    const {
      login: { token },
    } = getState()
    // 1,判断是否登录
    if (!!token) {
      // 2,登录删除rudex中的状态与本地
      await http.delete(`/user/channels/${channel.id}`)
    } else {
      // 3,未登录的获取本地存储删除对应数据
      // 4,返回存储至本地

      const locaState = JSON.parse(
        localStorage.getItem(CHANNEL_KEY) ?? '[]'
      ) as Channel[]
      const newChannels = locaState.filter((item) => item.id !== channel.id)

      localStorage.setItem(CHANNEL_KEY, JSON.stringify(newChannels))
    }
    dispatch({ type: 'Home/delMyChannels', payload: channel })
  }
}

// 添加我的频道

export const addChannels = (channel: Channel): RootThunkAction => {
  return async (dispatch, getState) => {
    const {
      login: { token },
    } = getState()
    // 1.判断是否登录
    // 2.登录发送请求更新
    if (!!token) {
      await http.patch('/user/channels', {
        channels: [channel],
      })
    } else {
      // 3.未登录,取出本地添加并反存至本地存储之中
      // console.log('未登录')

      const locaState = JSON.parse(
        localStorage.getItem(CHANNEL_KEY) || '[]'
      ) as Channel[]

      const newList = [...locaState, channel]

      localStorage.setItem(CHANNEL_KEY, JSON.stringify(newList))
      dispatch({ type: 'Home/addChannels', payload: channel })
    }
  }
}

//
